﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MOD;
using DAL;

namespace BLL
{
    public class B_AdInfo
    {
        private int iDataConn = DataBase.IN_S2;
        public DataTable SelectUserIdPassWord(long iUserId, long iWeiBoId)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Select * from ad_info ");
            sCmdText.Append("where UserId = @UserId  ");
            sCmdText.Append("and WeiboId = @WeiboId  ");
            SqlParameter[] sSp = {
                                    new SqlParameter("@UserId",SqlDbType.BigInt),
                                    new SqlParameter("@WeiboId",SqlDbType.BigInt)
                                 };
            sSp[0].Value = iUserId;
            sSp[1].Value = iWeiBoId;
            return dDb.GetDataTable(sCmdText.ToString(), iDataConn, sSp);
        }
        public DataTable SelectUserIdCount(long iUserId)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Select *  from ad_info ");
            sCmdText.Append("where UserId = @UserId  ");
            SqlParameter[] sSp = {
                                    new SqlParameter("@UserId",SqlDbType.BigInt)
                                 };
            sSp[0].Value = iUserId;

            return dDb.GetDataTable(sCmdText.ToString(), iDataConn, sSp);
        }
        public DataTable SelectOtherUserId(long iUserId)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Select count(*) as count from ad_info  ");
            sCmdText.Append("left join ad_repost on (ad_info.WeiboId = ad_repost.Adid ");
            sCmdText.Append("and UserId = @UserId) ");
 
            SqlParameter[] sSp = {
                                    new SqlParameter("@UserId",SqlDbType.BigInt)
                                 };
            sSp[0].Value = iUserId;

            return dDb.GetDataTable(sCmdText.ToString(), iDataConn, sSp);
        }
        public DataTable Select(Int64 iUserId, int iCount)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Select top ");
            sCmdText.Append( iCount.ToString() );
            sCmdText.Append(" * from ad_info ");
          //  sCmdText.Append(" where  ");
            sCmdText.Append(" order by createTime desc");
           
            SqlParameter[] sSp = {
                                    new SqlParameter("@Count",SqlDbType.Int)
     
                                 };
            sSp[0].Value = iCount;
  
            return dDb.GetDataTable(sCmdText.ToString(), iDataConn, sSp);
        }
        public int Insert( M_Adinfo mAo)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Insert into ad_info ");
            sCmdText.Append("(WeiboId,UserId,[CreateTime] )");
            sCmdText.Append("values");
            sCmdText.Append("(@WeiboId,@UserId,@CreateTime)");

            SqlParameter[] sSp = {
                                   new SqlParameter("@WeiboId",SqlDbType.BigInt),
                                   new SqlParameter("@UserId",SqlDbType.BigInt),
                                   new SqlParameter("@CreateTime",SqlDbType.DateTime)           
                                };
            sSp[0].Value = mAo.WeiboId;
            sSp[1].Value = mAo.UserId;
            sSp[2].Value = mAo.CreateTime;

            return dDb.ExecuteCmd(sCmdText.ToString(), iDataConn, sSp);
        }
    }
}
